import operator
import os

import pandas as pd
import numpy as np


class Data:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z


# 定义排序函数
def sort_by_attribute(data, attribute):
    # 单属性排序
    sorted_data = sorted(data, key=operator.attrgetter(attribute))
    return sorted_data


# 读取 Excel 文件的某一列数据
def read_excel_first_row(file_path, line):
    Sdata = []
    try:
        # 读取 Excel 文件
        data = pd.read_excel(file_path)
        # 获取一整列的数据
        column_data = data.iloc[:, line]

        # 使用 dropna() 方法去除 NaN 值
        first_row_data = column_data.dropna().tolist()
        # 获取某一列数据并转换为列表（3的话代表第四列）
        for item in first_row_data:
            values = item.split(',')  # 按逗号分隔数据
            x, y, z = map(float, values)  # 转换为浮点数
            data = Data(x, y, z)
            Sdata.append(data)  # 添加到 Pdata 列表中

        return Sdata
    except Exception as e:
        print(f"读取文件出错: {e}")
        return None





def evaluate_data(data_object, c1, d, c2, c3):
    if data_object.x > c1:
        return True
    elif data_object.x < d:
        return False
    else:
        if data_object.y > c2 or data_object.z > c3:
            return True
        else:
            return False


def count_results(data_list, c1, d, c2, c3):
    true_count = 0
    false_count = 0
    for data_object in data_list:
        result = evaluate_data(data_object, c1, d, c2, c3)
        if result:
            true_count += 1
        else:
            false_count += 1
    return [true_count, false_count]

def verify(file_path):
    # file_path = './output_new_3.xlsx'  # 修改为你的文件路径
    Sdata = read_excel_first_row(file_path, 0)
    Cdata = read_excel_first_row(file_path, 1)
    Ddata = read_excel_first_row(file_path, 2)

    # 选择需要排序的属性
    attribute_to_sort = 'x'  # 更改为 'x' 或 'z' 以按照不同属性排序

    # 使用单属性排序函数对 Pdata 列表排序
    sorted_Sdata = sort_by_attribute(Sdata, attribute_to_sort)

    attribute_to_sort = 'x'  # 更改为 'x' 或 'z' 以按照不同属性排序
    sorted_Ddata = sort_by_attribute(Ddata, attribute_to_sort)
    # 打印 Pdata 中的对象
    # for data in sorted_Pdata:
    #     print(f"x: {data.x}, y: {data.y}, z: {data.z}")

    # 获取 Ddata 中 x 的第 85 分位点值
    x_values_Ddata = [data.x for data in sorted_Ddata]
    d = np.percentile(x_values_Ddata, 85)

    attribute_to_sort = 'x'  # 更改为 'x' 或 'z' 以按照不同属性排序
    sorted_Cdata = sort_by_attribute(Cdata, attribute_to_sort)
    # 获取 cdata 中 x 的第 95 分位点值
    x_values_Cdata = [data.x for data in sorted_Cdata]
    c1 = np.percentile(x_values_Cdata, 95)

    # 打印第 85 分位点值和第 95 分位点值
    print(f"Ddata中x列第85分位点的值d：{d}")
    print(f"Cdata中x列第95分位点的值c：{c1}")

    # 筛选出 cdata 中 x 大于 d 且小于 c 的对象
    filtered_cdata = [data for data in Cdata if d < data.x < c1]

    sort_filtered_c2data = sort_by_attribute(filtered_cdata, 'y')
    y_values_Cdata = [data.y for data in sort_filtered_c2data]
    # print("Cdata y 列长度:", len(y_values_Cdata))
    c2 = np.percentile(y_values_Cdata, 95)
    print(f"Cdata中y列第95分位点的值：{c2}")

    sort_filtered_c3data = sort_by_attribute(filtered_cdata, 'z')
    z_values_Cdata = [data.z for data in sort_filtered_c3data]
    # print("len(z_values_Cdata):", len(z_values_Cdata))
    c3 = np.percentile(z_values_Cdata, 95)
    print(f"Cdata中z列第95分位点的值：{c3}")

    print(count_results(Sdata, c1, d, c2, c3))
    print(count_results(Cdata, c1, d, c2, c3))
    print(count_results(Ddata, c1, d, c2, c3))

    return [count_results(Sdata, c1, d, c2, c3),
            count_results(Cdata, c1, d, c2, c3),
            count_results(Ddata, c1, d, c2, c3)]

def verify_2(file_path_me):
    file_path = './output_new_3.xlsx'  # 修改为你的文件路径
    Sdata = read_excel_first_row(file_path, 0)
    Cdata = read_excel_first_row(file_path, 1)
    Ddata = read_excel_first_row(file_path, 2)

    # 选择需要排序的属性
    attribute_to_sort = 'x'  # 更改为 'x' 或 'z' 以按照不同属性排序

    # 使用单属性排序函数对 Pdata 列表排序
    sorted_Sdata = sort_by_attribute(Sdata, attribute_to_sort)

    attribute_to_sort = 'x'  # 更改为 'x' 或 'z' 以按照不同属性排序
    sorted_Ddata = sort_by_attribute(Ddata, attribute_to_sort)
    # 打印 Pdata 中的对象
    # for data in sorted_Pdata:
    #     print(f"x: {data.x}, y: {data.y}, z: {data.z}")

    # 获取 Ddata 中 x 的第 85 分位点值
    x_values_Ddata = [data.x for data in sorted_Ddata]
    d = np.percentile(x_values_Ddata, 85)

    attribute_to_sort = 'x'  # 更改为 'x' 或 'z' 以按照不同属性排序
    sorted_Cdata = sort_by_attribute(Cdata, attribute_to_sort)
    # 获取 cdata 中 x 的第 95 分位点值
    x_values_Cdata = [data.x for data in sorted_Cdata]
    c1 = np.percentile(x_values_Cdata, 95)

    # 打印第 85 分位点值和第 95 分位点值
    print(f"Ddata中x列第85分位点的值d：{d}")
    print(f"Cdata中x列第95分位点的值c：{c1}")

    # 筛选出 cdata 中 x 大于 d 且小于 c 的对象
    filtered_cdata = [data for data in Cdata if d < data.x < c1]

    sort_filtered_c2data = sort_by_attribute(filtered_cdata, 'y')
    y_values_Cdata = [data.y for data in sort_filtered_c2data]
    # print("Cdata y 列长度:", len(y_values_Cdata))
    c2 = np.percentile(y_values_Cdata, 95)
    print(f"Cdata中y列第95分位点的值：{c2}")

    sort_filtered_c3data = sort_by_attribute(filtered_cdata, 'z')
    z_values_Cdata = [data.z for data in sort_filtered_c3data]
    # print("len(z_values_Cdata):", len(z_values_Cdata))
    c3 = np.percentile(z_values_Cdata, 95)
    print(f"Cdata中z列第95分位点的值：{c3}")


    Sd = read_excel_first_row(file_path_me, 0)
    Cd = read_excel_first_row(file_path_me, 1)
    Dd = read_excel_first_row(file_path_me, 2)

    print(count_results(Sd, c1, d, c2, c3))
    print(count_results(Cd, c1, d, c2, c3))
    print(count_results(Dd, c1, d, c2, c3))

    return [count_results(Sd, c1, d, c2, c3),
            count_results(Cd, c1, d, c2, c3),
            count_results(Dd, c1, d, c2, c3)]